#include "../tools.h"

// 难点在于不使用 long 判断越界
// res > boundp || res < boundn
// while(x != 0) // 不将负数转正处理，x /= 10 终将汇聚为 0

class Solution {
public:
    int reverse(int x) {
        int res = 0;
        int boundp = INT32_MAX / 10;
        int boundn = INT32_MIN / 10;

        while(x != 0){
            if(res > boundp || res < boundn) return 0;
            int bit = x % 10;
            res = res * 10 + bit;
            x /= 10;
        }
        
        return res;
    }
};

int main(int argc, char const *argv[])
{
    Solution s;

    int x = -123;

    cout << s.reverse(x) << endl;

    return 0;
}
